import { Metadata } from "next"

import { Separator } from "@/components/ui/separator"
import { SidebarNav } from "@/components/layout/sidebar-nav"
import { getBiddingById, getBiddingConditions } from "@/lib/bidding/service"
import { Bidding } from "@/db/schema/bidding"
import { Button } from "@/components/ui/button"
import { ArrowLeft } from "lucide-react"
import Link from "next/link"
import { BiddingInfoHeader } from "@/components/bidding/bidding-info-header"
import { BiddingConditionsEdit } from "@/components/bidding/bidding-conditions-edit"
export const metadata: Metadata = {
  title: "Bidding Detail",
}

export default async function SettingsLayout({
  children,
  params,
}: {
  children: React.ReactNode
  params: { lng: string , id: string}
}) {

  // 1) URL 파라미터에서 id 추출, Number로 변환
  const resolvedParams = await params
  const lng = resolvedParams.lng
  const id = resolvedParams.id

  const idAsNumber = Number(id)
  // 2) DB에서 해당 입찰 정보 조회
  const bidding: Bidding | null = await getBiddingById(idAsNumber)
  const biddingConditions = await getBiddingConditions(idAsNumber)

  // 3) 사이드바 메뉴
  const sidebarNavItems = [
    {
      title: "입찰 사전견적",
      href: `/${lng}/evcp/bid/${id}/pre-quote`,
    },
    {
      title: "입찰 관리상세",
      href: `/${lng}/evcp/bid/${id}/detail`,
    },
  ]

  return (
    <>
      <div className="container py-6">
        <section className="overflow-hidden rounded-[0.5rem] border bg-background shadow">
          <div className="hidden space-y-6 p-10 pb-16 md:block">
            {/* RFQ 목록으로 돌아가는 링크 추가 */}
            <div className="flex justify-between items-center mb-4">
              <div>
                {/* 4) 입찰 정보가 있으면 번호 + 제목 + "상세 정보" 표기 */}
                <h2 className="text-2xl font-bold tracking-tight">
                  {bidding
                    ? `입찰 No. ${bidding.biddingNumber ?? ""} - ${bidding.title}`
                    : "Loading Bidding..."}
                </h2>
              </div>
              <Link href={`/${lng}/evcp/bid`} passHref>
                <Button variant="ghost" className="flex items-center text-primary hover:text-primary/80 transition-colors p-0 h-auto">
                  <ArrowLeft className="mr-1 h-4 w-4" />
                  <span>입찰 목록으로 돌아가기</span>
                </Button>
              </Link>
            </div>

            {/* 입찰 정보 헤더 */}
            <BiddingInfoHeader bidding={bidding} />
            
            {/* 입찰 조건 */}
            {bidding && (
              <BiddingConditionsEdit
                biddingId={bidding.id}
                initialConditions={biddingConditions}
              />
            )}
            
            <Separator className="my-6" />
            <div className="flex flex-col space-y-8 lg:flex-row lg:space-x-12 lg:space-y-0">
              <aside className="-mx-4 lg:w-1/5">
                <SidebarNav items={sidebarNavItems} />
              </aside>
              <div className="flex-1 overflow-auto max-w-full">{children}</div>
            </div>
          </div>
        </section>
      </div>
    </>
  )
}